

package com.maoshi.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.common.annotation.SysLog;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.distribution.comment.model.DistributionWithdrawCashSet;
import com.maoshi.shop.distribution.comment.service.DistributionWithdrawCashSetService;
import com.maoshi.shop.security.platform.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * 提现设置
 *
 * @author xwc
 * @date 2019-04-13 15:45:12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/distributionWithdrawCashSet")
public class DistributionWithdrawCashSetController {

    private final DistributionWithdrawCashSetService distributionWithdrawCashSetService;

    /**
     * 分页查询
     *
     * @param page                        分页对象
     * @param distributionWithdrawCashSet 提现设置
     * @return 分页数据
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:page')")
    public ResponseEntity<IPage<DistributionWithdrawCashSet>> getDistributionWithdrawCashSetPage(PageParam<DistributionWithdrawCashSet> page, DistributionWithdrawCashSet distributionWithdrawCashSet) {
        return ResponseEntity.ok(distributionWithdrawCashSetService.page(page, new LambdaQueryWrapper<DistributionWithdrawCashSet>()));
    }

    /**
     * 通过id查询提现设置
     *
     * @param id id
     * @return 单个数据
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:info')")
    public ResponseEntity<DistributionWithdrawCashSet> getById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(distributionWithdrawCashSetService.getById(id));
    }

    /**
     * 通过shopId查询提现设置
     *
     * @return 单个数据
     */
    @GetMapping("/info")
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:info')")
    public ResponseEntity<DistributionWithdrawCashSet> getByShopId() {
        return ResponseEntity.ok(distributionWithdrawCashSetService.getByShopId());
    }

    /**
     * 新增提现设置
     *
     * @param distributionWithdrawCashSet 提现设置
     * @return 是否新增成功
     */
    @SysLog("新增提现设置")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:save')")
    public ResponseEntity<Boolean> save(@RequestBody @Valid DistributionWithdrawCashSet distributionWithdrawCashSet) {
        distributionWithdrawCashSet.setUpdateTime(new Date());
        distributionWithdrawCashSet.setShopId(Constant.PLATFORM_SHOP_ID);
        distributionWithdrawCashSet.setModifier(SecurityUtils.getSysUser().getUserId());
        return ResponseEntity.ok(distributionWithdrawCashSetService.save(distributionWithdrawCashSet));
    }

    /**
     * 修改提现设置
     *
     * @param distributionWithdrawCashSet 提现设置
     * @return 是否修改成功
     */
    @SysLog("修改提现设置")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:update')")
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid DistributionWithdrawCashSet distributionWithdrawCashSet) {
        distributionWithdrawCashSet.setUpdateTime(new Date());
        distributionWithdrawCashSet.setModifier(SecurityUtils.getSysUser().getUserId());
        distributionWithdrawCashSet.setShopId(Constant.PLATFORM_SHOP_ID);
        distributionWithdrawCashSetService.updateById(distributionWithdrawCashSet);

        // 移除缓存
        distributionWithdrawCashSetService.removeCacheByShopId(Constant.PLATFORM_SHOP_ID);
        return ResponseEntity.ok().build();
    }

    /**
     * 通过id删除提现设置
     *
     * @param id id
     * @return 是否删除成功
     */
    @SysLog("删除提现设置")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('plateform:distributionWithdrawCashSet:delete')")
    public ResponseEntity<Boolean> removeById(@PathVariable Long id) {
        return ResponseEntity.ok(distributionWithdrawCashSetService.removeById(id));
    }

}
